# -*- coding: utf-8 -*-
"""
Module Description:
Date: 2017/5/25
Author:
"""
from functools import wraps

from pyshare.const.lang.tips_info import TipsInfo
from common.service_config import get_remote_service_switch
from pyshare.const.enums.remote_service_switch import ServiceSwitch


def remote_service_switch(switch_id, return_info=True):
    def return_info_check(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            action_object = args[0]

            switch = get_remote_service_switch(switch_id)
            info = ''
            if switch_id == ServiceSwitch.Chat:
                # 聊天开关
                info = TipsInfo.ChatKeys.chatServiceBrokenDown
            elif switch_id == ServiceSwitch.Mail:
                # 邮件开关
                info = TipsInfo.MailKeys.mailServiceDisable
            elif switch_id == ServiceSwitch.Redeem:
                # 兑换码开关
                info = TipsInfo.RedeemKeys.redeemServiceDisable
            elif switch_id == ServiceSwitch.Hunt:
                # 皇家围猎开关
                info = TipsInfo.HuntKeys.huntActionDisable
            elif switch_id == ServiceSwitch.ReusableActivity:
                # 万能活动开关
                info = TipsInfo.ReusableActivityKeys.reusableActivityActionDisable
            elif switch_id == ServiceSwitch.CrossLeaderBoard:
                info = TipsInfo.LeaderBoardKeys.WaitForOpen
            elif switch_id == ServiceSwitch.AllianceBattle:
                info = TipsInfo.AllianceBattleKeys.AllianceBattleNotOpen
            elif switch_id == ServiceSwitch.ImperialGarden:
                info = TipsInfo.LeaderBoardKeys.WaitForOpen
            elif switch_id == ServiceSwitch.Alliance:
                info = TipsInfo.AllianceKeys.AllianceTransferredDisable
            elif switch_id == ServiceSwitch.SchoolOfEmperor:
                info = TipsInfo.PrinceKeys.SchoolOfEmperorDisable
            elif switch_id == ServiceSwitch.AntiAddictionSystem:
                info = TipsInfo.AntiAddiction.AntiAddictionSystemDisable
            elif switch_id == ServiceSwitch.GrowthPlan:
                info = TipsInfo.GrowthPlan.GrowthPlanDisable
            elif switch_id == ServiceSwitch.Certification:
                info = TipsInfo.ChatKeys.CanNotSentMsgWithoutRealNameAuthentication
            elif switch_id == ServiceSwitch.PowerFund:
                info = TipsInfo.FundKeys.PowerFundNotOpen
            elif switch_id == ServiceSwitch.AllianceCancelApply:
                info = TipsInfo.AllianceKeys.CancelApplyDisable
            elif switch_id == ServiceSwitch.Interaction:
                info = TipsInfo.GlobalKeys.actionUnavailable
            elif switch_id == ServiceSwitch.VassalTributeTimes:
                info = TipsInfo.GlobalKeys.actionUnavailable

            if switch:
                return func(*args, **kwargs)
            elif return_info:
                action_object.set_error(info)
                return False
            else:
                # 类似18004，客户端主动频繁发送的请求，不记录到Kibana
                return True
        return wrapper
    return return_info_check


def is_cross_leader_board_open():
    """
    返回跨服榜单开关
    :return:
    """
    return get_remote_service_switch(ServiceSwitch.CrossLeaderBoard)


def is_cross_server_online(cross_server='cross'):
    """
    跨服服务是否开启
    :return:
    """
    from pyshare.const.enums.server_info import ServerRunningStatus
    from common.zk_config import CrossServerStatus
    from common.service_config import get_app_id
    cross_server_status_dict = CrossServerStatus.get_config_data()

    app_server = str(get_app_id()) + ':' + str(cross_server)
    status = cross_server_status_dict.get(app_server, None)
    return True if status == ServerRunningStatus.Online else False


def cross_service_switch(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        action_object = args[0]
        if is_cross_server_online():
            return func(*args, **kwargs)
        else:
            action_object.set_error(TipsInfo.CrossActivity.CrossServiceError)
            return False

    return wrapper



def is_siege_use_sql():
    """
    跨服攻城战是否使用原生sql
    :return:
    """
    return get_remote_service_switch(ServiceSwitch.SiegeUseSql)


def is_siege_use_push():
    """
    跨服攻城战是否使用血量推送
    :return:
    """
    return get_remote_service_switch(ServiceSwitch.SiegeUsePush)


def is_device_push_open():
    """
    设备推送是否开启
    :return:
    """
    return get_remote_service_switch(ServiceSwitch.DevicePush)


def is_check_register_limit_open():
    """是否校验注册上限异常"""
    return get_remote_service_switch(ServiceSwitch.RegisterMaxLimit)


def is_open_dts():
    """是否使用DTS"""
    return get_remote_service_switch(ServiceSwitch.DTS)


def is_region_route_open():
    """是否访问区域路由服务"""
    return get_remote_service_switch(ServiceSwitch.RegionRoute)


def is_display_month_recharge():
    """是否下发每月充值"""
    return get_remote_service_switch(ServiceSwitch.DisplayMonthRecharge)
